<!DOCTYPE html>
<html class="x-admin-sm">

<head>
    <meta charset="UTF-8">
    <title>新增</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <link rel="stylesheet" href="/web/res/css/xadmin.css">
    <script src="/web/res/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="/web/res/js/xadmin.js"></script>
</head>

<body style="background-color: white;">
<div class="layui-fluid" id="orderEdit">
    <form class="layui-form layui-form-pane" id="formsubmit" style="width: 700px;margin:20px auto"
          action="/after/agent/save">
        <div class="layui-form-item">
            <label class="layui-form-label" style="width: 140px">选择地区</label>
            <div class="layui-inline" style="width: 164px">
                <select class="province" lay-verify="required" lay-search lay-filter="province">
                    <option value="">省份</option>
                </select>
            </div>
            <div class="layui-inline" style="width: 164px">
                <select class="city" lay-verify="required" lay-search lay-filter="city">
                    <option value="">地级市</option>
                </select>
            </div>
            <div class="layui-inline" style="width: 164px">
                <select name="areaId" class="district" lay-verify="required" lay-search>
                    <option value="">县/区</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" style="width: 140px">账号</label>
            <div class="layui-input-block" style="margin-left: 140px">
                <input type="text" name="username" required lay-verify="required" autocomplete="off"
                       class="layui-input"
                       value=""/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" style="width: 140px">密码</label>
            <div class="layui-input-block" style="margin-left: 140px">
                <input type="text" name="password" required lay-verify="required" autocomplete="off"
                       class="layui-input"
                       value=""/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" style="width: 140px">充值手续费比例</label>
            <div class="layui-input-block" style="margin-left: 140px">
                <input type="number" name="rechargeRate" required lay-verify="required" autocomplete="off"
                       class="layui-input"
                       value=""/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" style="width: 140px">提现手续费比例</label>
            <div class="layui-input-block" style="margin-left: 140px">
                <input type="text" name="withdrawRate" required lay-verify="required" autocomplete="off"
                       class="layui-input"
                       value=""/>
            </div>
        </div>
<!--        <div class="layui-form-item">-->
<!--            <label class="layui-form-label" style="width: 140px">余额</label>-->
<!--            <div class="layui-input-block" style="margin-left: 140px">-->
<!--                <input type="number" name="money" required lay-verify="required" autocomplete="off"-->
<!--                       class="layui-input"-->
<!--                       value=""/>-->
<!--            </div>-->
<!--        </div>-->
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="button" class="layui-btn layui-btn-radius" lay-submit lay-filter="save">&emsp;保存&emsp;
                </button>
                <button type="button" class="layui-btn layui-btn-primary layui-btn-radius" onclick="xadmin.close()">
                    &emsp;返回&emsp;
                </button>
            </div>
        </div>
    </form>
</div>
<script>
    let areaData;
    getArea()
    function getArea() {
        layui.use('layer',function () {
            let $ = layui.$;
            ajaxPost($,"/after/agent/sslist",{},function (data) {
                if(data.code==0){
                    areaData = data.data.list
                    console.log(areaData)
                }
            });
        });
    }
    layui.use([ 'form', 'element' ], function() {
        let $ = layui.$;
        var form = layui.form,
            element = layui.element;

        var province = $(".province");

        //初始将省份数据赋予
        for (var i = 0; i < areaData.length; i++) {
            addEle(province, areaData[i].province,areaData[i].provinceid);
        }

        //向select中 追加内容
        function addEle(ele, value, id) {
            var optionStr = "";
            optionStr = "<option value=" + id + " >" + value + "</option>";
            ele.append(optionStr);
        }
        //重新渲染select
        form.render('select');

        //移除select中所有项
        function removeEle(ele) {
            ele.find("option").remove();
            var optionStar = "<option value=''>" + "请选择" + "</option>";
            ele.append(optionStar);
        }

        var provinceText,
            cityText,
            cityItem;
        //选定省份后 将该省份的数据读取追加上
        form.on('select(province)', function(data) {
            console.log(data)
            var city = $(data.elem).parents(".layui-form-item").find(".city"),
                district = $(data.elem).parents(".layui-form-item").find(".district");
            provinceText = data.value;
            $.each(areaData, function(i, item) {
                if (provinceText == item.provinceid) {
                    cityItem = i;
                    return cityItem;
                }
            });
            console.log(cityItem)
            removeEle(city);
            removeEle(district);
            $.each(areaData[cityItem].citiesList, function(i, item) {
                addEle(city, item.city, item.cityid || item.id);
            })
            form.render('select');
        })

        //选定后 将对应的数据读取追加上
        form.on('select(city)', function(data) {
            var district = $(data.elem).parents(".layui-form-item").find(".district");
            cityText = data.value;
            console.log(cityText)
            removeEle(district);
            $.each(areaData, function(i, item) {
                if (provinceText == item.provinceid) {
                    cityItem = i;
                    return cityItem;
                }
            });
            $.each(areaData[cityItem].citiesList, function(i, item) {
                if (cityText == item.cityid) {
                    for (var n = 0; n < item.areaList.length; n++) {
                        addEle(district, item.areaList[n].name,item.areaList[n].id);
                    }
                }
            })
            form.render('select');
        })



    })
    layui.use(['form', 'upload'], function () {
        let $ = layui.$;
        var form = layui.form;
        var upload = layui.upload;
        form.render();
        // 重新添加删除功能
        $('.upload-cancel').on('click', (event) => {
            let self = $(event.currentTarget);
            self.parent().remove();
            event.stopPropagation();

            if ($("input[id='imgpathhide']").length < 1) {
                $('#uploadtemplate').show();
            } else {
                $('#uploadtemplate').hide();
            }
        });
        upload.render({
            elem: '#uploadtemplate',
            // auto: false,
            url: '/after/family/uppic',
            accept: 'images',
            acceptMime: 'image/*',
            choose: function (obj) {
            },
            done: function (res) {
                console.log(res)
                $('#uploadlist').prepend(
                    '<div class="upload-imgBox">  <input type="hidden"  id="imgpathhide" value="' + res.data + '"  class="layui-input"/><div class="upload-cancel">x</div><img  src="' + res.data + '" class="upload-select-img"></div>');
                $(".upload-cancel").on("click", function (e) {
                    $(this).parent().remove();
                    if ($("input[id='imgpathhide']").length < 1) {
                        $('#uploadtemplate').show();
                    }
                })
                if ($("input[id='imgpathhide']").length >= 1) {
                    $('#uploadtemplate').hide();
                }
            }
        });

        // upload.render({
        //     elem: '#uploadtemplate',
        //     auto: false,
        //     accept: 'images',
        //     acceptMime: 'image/*',
        //     choose: function (obj) {
        //         obj.preview(function (index, file, result) {
        //             $('#uploadlist').prepend('<div class="upload-imgBox">  <input type="hidden"  id="imgpathhide" value="' + result + '"  class="layui-input"/><div class="upload-cancel" id="' + index + '">x</div><img  src="' + result + '" alt="' + file.name +
        //                     '" class="upload-select-img"></div>');
        //             $(".upload-cancel").on("click", function (e) {
        //                 $(this).parent().remove();
        //                 if ($("input[id='imgpathhide']").length < 1) {
        //                     $('#uploadtemplate').show();
        //                 }
        //             })
        //             if ($("input[id='imgpathhide']").length >= 1) {
        //                 $('#uploadtemplate').hide();
        //             }
        //             // alert($("input[id='imgpathhide']").length);
        //         });
        //     },
        //     done: function (res) {

        //     }
        // });

        $.fn.extend({
            serializeObject: function () {
                var obj = {}; //声明一个对象
                $.each(this.serializeArray(), function (index, field) {
                    obj[field.name] = field.value; //通过变量，将属性值，属性一起放到对象中
                })
                return obj;
            }
        });
        form.on('submit(' + $("button[lay-submit]:eq(0)").attr("lay-filter") + ')', function (data) {

            // $("input[id='imgpathhide']").each(function (index, ele) {
            //     $(this).attr("name", "pic");
            // });
            layer.confirm('确认要保存吗？', function (index) {

                var loading = layer.load(0, {
                    shade: true
                });
                ajaxPost($, $("#formsubmit").attr("action"), $("#formsubmit").serializeObject(), function (data) {
                    layer.close(loading);
                    if (data.code == 0) {

                        layer.msg(data.msg, {icon: 1, time: 1000}, function () {
                            xadmin.father_reload();
                        });
                    } else {
                        layer.close(loading);
                        layer.msg(data.msg, {icon: 2, time: 3000}, function () {

                        });
                    }
                });
            });
        });
    });
</script>
</body>

</html>